package cn.edu.zut.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author Davey chan
 */
@WebServlet("/req2")
public class ServletURLcode extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        post方式解决中文乱码
        request.setCharacterEncoding("UTF-8");//设置字符输入流的编码
//获取username
        String username=request.getParameter("username");
        System.out.println("解决前"+username);
               /*GET,获取参数的方式和POST不一样是：getQueryStirng
        因为tomcat的解码方式不同，所以产生了乱码
        按照乱码的底层原理来解决
        先对乱码的数据进行解码：转为字节数组

        * */
//        byte[] bytes = username.getBytes(StandardCharsets.ISO_8859_1);
//        username = new String(bytes, StandardCharsets.UTF_8);
//        二合一，post也可以用
        username=new String(username.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        System.out.println("解决后的"+username);
}
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        和GET的方法一样，直接去找get方法
        doGet(request, response);
    }


}
